package com.saernuosi.adapter;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

import com.saernuosi.R;
import com.saernuosi.helper.ViewHolder;

import android.content.Context;
import android.opengl.Visibility;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.CheckBox;
import android.widget.ImageView;
import android.widget.TextView;

public class UploadListViewAdapter extends BaseAdapter {
	private Context context;
	private List<File> list;

	public UploadListViewAdapter(Context context, List<File> list) {
		this.context = context;
		if (list == null) {
			list = new ArrayList<File>();
		}
		this.list = list;
	}

	@Override
	public int getCount() {
		// TODO Auto-generated method stub
		return list.size();
	}

	@Override
	public Object getItem(int position) {
		// TODO Auto-generated method stub
		return list.get(position);
	}

	@Override
	public long getItemId(int position) {
		// TODO Auto-generated method stub
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		ViewHolder vh = null;
		if (convertView == null) {
			convertView = LayoutInflater.from(context).inflate(
					R.layout.item_listview_upload, null);
			vh = new ViewHolder();
			vh.tvName = (TextView) convertView
					.findViewById(R.id.upload_listview_filename);
			vh.cb = (CheckBox) convertView
					.findViewById(R.id.upload_listview_checkbox);
			vh.ivIcon = (ImageView) convertView.findViewById(R.id.upload_listview_fileicon);
			convertView.setTag(vh);
		}else{
			vh = (ViewHolder) convertView.getTag();
		}
		if(list.get(position).isDirectory()){
			vh.ivIcon.setBackgroundResource(R.drawable.cloud_dir_icon);
			vh.cb.setVisibility(View.INVISIBLE);
		}
		if(list.get(position).getName().endsWith(".txt")){
			vh.ivIcon.setBackgroundResource(R.drawable.ft_txt);
		}
		if(list.get(position).getName().endsWith(".mp3")){
			vh.ivIcon.setBackgroundResource(R.drawable.ft_mp3);
		}
		if(list.get(position).getName().endsWith(".png")){
			vh.ivIcon.setBackgroundResource(R.drawable.ft_png);
		}
		if(list.get(position).getName().endsWith(".rar")){
			vh.ivIcon.setBackgroundResource(R.drawable.ft_rar);
		}
		if(list.get(position).getName().endsWith(".zip")){
			vh.ivIcon.setBackgroundResource(R.drawable.ft_zip);
		}
		if(list.get(position).getName().endsWith(".ppt")){
			vh.ivIcon.setBackgroundResource(R.drawable.ft_ppt);
		}
		if(list.get(position).getName().endsWith(".pptx")){
			vh.ivIcon.setBackgroundResource(R.drawable.ft_pptx);
		}
		if(list.get(position).getName().endsWith(".bat")){
			vh.ivIcon.setBackgroundResource(R.drawable.ft_bat);
		}
		if(list.get(position).getName().endsWith(".bin")){
			vh.ivIcon.setBackgroundResource(R.drawable.ft_bin);
		}
		vh.tvName.setText(list.get(position).getName());
		return convertView;
	}
	public void OnChangeData(){
		this.notifyDataSetChanged();
	}
	/*class ViewHolder {
		TextView tvName;
		CheckBox cb;
	}*/
}
